<?php


namespace app\service;


use think\Db;

class logService
{

    public function deleteRoom($roominfo,$type,$user_id,$user_name){
        $log["parkingNo"]=$roominfo["parkingNo"];
        $log["pid"]=$roominfo["id"];
        $log["action"]="delete";
        $log["actionType"]=$type;
        $log["created_id"]=$user_id;
        $log["created_name"]=$user_name;
        $log["con"]=json_encode($roominfo);
        $log["type"]="room";
        $log["create_time"]=date("Y-m-d H:i:s");
        Db::table("cxyun_log")->insert($log);
    }
    public function deleteUser($user,$parkingNo,$type,$user_id,$user_name){
        $log["parkingNo"]=$parkingNo;
        $log["pid"]=$user["id"];
        $log["action"]="delete";
        $log["actionType"]=$type;
        $log["created_id"]=$user_id;
        $log["created_name"]=$user_name;
        $log["con"]=json_encode($user);
        $log["create_time"]=date("Y-m-d H:i:s");
        $log["type"]="user";
        Db::table("cxyun_log")->insert($log);
        Db::table("userinfo")->where("id",$user["id"])->delete();
    }

    public function deleteCar($carinfo,$type,$user_id,$user_name,$remarks=""){
        $log["parkingNo"]=$carinfo["parkingNo"];
        $log["pid"]=$carinfo["id"];
        $log["action"]="delete";
        $log["actionType"]=$type;
        $log["created_id"]=$user_id;
        $log["created_name"]=$user_name;
        $log["con"]=json_encode($carinfo);
        $log["create_time"]=date("Y-m-d H:i:s");
        $log["type"]="car";
        $log["remarks"]=$remarks;
        Db::table("cxyun_log")->insert($log);
        Db::table("cxyun_car")->where("id",$carinfo["id"])->delete();
        Db::table("cxyun_car_delete")->insert($carinfo);
    }

    public function deletePklot($pklotInfo,$type,$user_id,$user_name){
        $log["parkingNo"]=$pklotInfo["parkingNo"];
        $log["pid"]=$pklotInfo["id"];
        $log["action"]="delete";
        $log["actionType"]=$type;
        $log["created_id"]=$user_id;
        $log["created_name"]=$user_name;
        $log["con"]=json_encode($pklotInfo);
        $log["create_time"]=date("Y-m-d H:i:s");
        $log["type"]="pklot";
        Db::table("cxyun_log")->insert($log);


        $roomList=Db::table("roominfo")->where("pklot",$pklotInfo["pklot"])->select();
        foreach ($roomList as $room){
            $log["parkingNo"]=$pklotInfo["parkingNo"];
            $log["pid"]=$pklotInfo["id"];
            $log["action"]="update";
            $log["actionType"]=$type;
            $log["created_id"]=$user_id;
            $log["created_name"]=$user_name;
            $log["con"]=json_encode($room);
            $log["create_time"]=date("Y-m-d H:i:s");
            $log["type"]="roominfo";
            $log["remarks"]="更新房屋车位组为空";
            Db::table("roominfo")->where("id",$room["id"])->update(["pklot"=>""]);
            Db::table("cxyun_log")->insert($log);

            //查询该房屋下是否有车牌
            $carList=Db::table("cxyun_car")->where("room_id",$room["id"])->select();

            foreach ($carList as $value){
                $this->deleteCar($value,"1",session("id"),session("name"),"用户清空车位组");
            }
        }

    }
}